package com.dnake.utils;

import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;


import com.dnake.entity.OpenHistoryBean;
import com.dnake.entity.UserEaseBean;
import com.dnake.handle.PhotoHandler;

import java.io.IOException;
import java.util.List;

/**
 * Created by haoyuliu on 2016/12/7.
 */

public class CameraUtil {
    public static final int CAMERA_FACING_BACK = 0;
    public static final int CAMERA_FACING_FRONT = 1;
    public static final int CAMERA_NONE = 2;
    public static Camera camera = null;

    /**
     * 打开摄像头
     * @return
     */
    public static Camera openFacingBackCamera() {

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    camera = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    if (camera!=null){
                        camera.stopPreview();
                        camera.release();
                    }
                    camera = null;
                    ToastUtil.show("拍照失败");
                    //TODO 找不到摄像头 尝试重启一下系统xi
                }
            }else if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                try {
                    camera = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    if (camera!=null){
                        camera.stopPreview();
                        camera.release();
                    }
                    camera = null;
                    ToastUtil.show("拍照失败");
                }
            }
        }

        return camera;
    }

    /**
     * 拍照
     * @param context
     * @param holder
     * @param usernames
     * @param type
     * @param userName
     * @param openHistoryBean
     */
    public static void takePicture(Context context, SurfaceHolder holder, List<UserEaseBean> usernames, int type, String userName, OpenHistoryBean openHistoryBean){
        //拍照片 传给app
        camera = CameraUtil.openFacingBackCamera();
        if (camera==null){
            try {
                Thread.sleep(500);
                camera = CameraUtil.openFacingBackCamera();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (camera != null) {
            Camera.Parameters parameters = camera.getParameters();// 获得相机参数
            List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
            List<Camera.Size> sizes_pic = parameters.getSupportedPictureSizes();
            Camera.Size cs = sizes.get(0);
            int mWidth = cs.width;
            int mHeight = cs.height;
            parameters.setPreviewSize(mWidth, mHeight);
            parameters.setPictureSize(sizes_pic.get(0).width, sizes_pic.get(0).height);
            parameters.setJpegQuality(85);//
            parameters.setPictureFormat(ImageFormat.JPEG);
            camera.setParameters(parameters);
            try {
                camera.setPreviewDisplay(holder);
                camera.startPreview();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                camera.takePicture(null, null, new PhotoHandler(context,usernames,type,userName,openHistoryBean));
            } catch (IOException e) {
                e.printStackTrace();
                camera.stopPreview();
                camera.release();
                Log.e("camera","拍照失败");
            }
            Log.e("camera","开始拍照");
        }else {
            Log.e("camera","未找到摄像头");
        }
    }

    public static int HasBackCamera()
    {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CAMERA_FACING_BACK) {
                return i;
            }
        }
        return 2;
    }

    public static int HasFrontCamera()
    {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CAMERA_FACING_FRONT) {
                return i;
            }
        }
        return 2;
    }

    /**
     * 获取摄像头的像素
     * @param paramInt
     * @return
     */
    public static String getCameraPixels(int paramInt)
    {
        if (paramInt == 2)
            return "无";
        Camera localCamera = Camera.open(paramInt);
        Camera.Parameters localParameters = localCamera.getParameters();
        localParameters.set("camera-id", 1);
        List<Camera.Size> localList = localParameters.getSupportedPictureSizes();
        if (localList != null)
        {
            int heights[] = new int[localList.size()];
            int widths[] = new int[localList.size()];
            for (int i = 0; i < localList.size(); i++)
            {
                Camera.Size size = (Camera.Size) localList.get(i);
                int sizehieght = size.height;
                int sizewidth = size.width;
                heights[i] = sizehieght;
                widths[i] =sizewidth;
            }
            int pixels = getMaxNumber(heights) * getMaxNumber(widths);
            localCamera.release();
            return String.valueOf(pixels / 10000) + " 万";

        }
        else return "无";

    }

    public static int getMaxNumber(int[] paramArray)
    {
        int temp = paramArray[0];
        for(int i = 0;i<paramArray.length;i++)
        {
            if(temp < paramArray[i])
            {
                temp = paramArray[i];
            }
        }
        return temp;
    }
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open(0);
        } catch (Exception e) {
            canUse = false;
        }
        if (canUse) {
            mCamera.release();
            mCamera = null;
        }

        return canUse;
    }
}
